package com.fansea.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.net.URLEncoder;

@Slf4j
public class TranslateUtil {

    public static String translate(String word){
        String translateWord = null;
        try {
            String result = HttpRequestUtil.doGet("https://api.oioweb.cn/api/txt/QQFanyi?sourceText=" +  URLEncoder.encode(word, "UTF-8"));
            JSONObject jsonObject = JSON.parseObject(result);
            String msg = jsonObject.getString("msg");
            String result1 = jsonObject.getString("result");
            JSONObject jsonObject1 = JSON.parseObject(result1);
            translateWord = jsonObject1.getString("targetText");
            if (msg!="success" && translateWord.equals(null)){
                translateWord="翻译系统出错，请联系管理员修复bug！";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return translateWord;
    }

    public static void main(String[] args) {
        System.out.println(translate("你好吗"));

    }
}
